//
//  UITableView+RegisterCell.m
//  OCPopular
//
//  Created by Jarvis on 2024/6/3.
//

#import "UITableView+RegisterCell.h"
#import "RecycleViewSection.h"


@implementation UITableView (RegisterCell)


- (void)registerCell:(NSArray<RecycleViewSection *> *)arr {
    NSMutableArray<NSDictionary<NSString *, Class> *> *headers = [NSMutableArray array];
    NSMutableArray<NSDictionary<NSString *, Class> *> *footers = [NSMutableArray array];
    NSMutableArray<NSDictionary<NSString *, Class> *> *cells = [NSMutableArray array];
    for (RecycleViewSection *section in arr) {
        RecycleViewBinder *header = section.header;
        RecycleViewBinder *footer = section.footer;
        NSArray<RecycleViewBinder *> *_cells = section.cells;
        if (header) {
            [headers addObject:@{header.identifier: header.className}];
        }
        if (footer) {
            [footers addObject:@{footer.identifier: footer.className}];
        }
        if (_cells.count > 0) {
            for (RecycleViewBinder *cell in _cells) {
                [cells addObject:@{cell.identifier: cell.className}];
            }
        }
    }
    for (NSDictionary *dict in headers) {
        NSString *identifier = dict.allKeys.firstObject;
        Class class = dict[identifier];
        [self registerClass:class forHeaderFooterViewReuseIdentifier:identifier];
    }
    for (NSDictionary *dict in footers) {
        NSString *identifier = dict.allKeys.firstObject;
        Class class = dict[identifier];
        [self registerClass:class forHeaderFooterViewReuseIdentifier:identifier];
    }
    for (NSDictionary *dict in cells) {
        NSString *identifier = dict.allKeys.firstObject;
        Class class = dict[identifier];
        [self registerClass:class forCellReuseIdentifier:identifier];
    }
}

@end
